package com.ccx.coachsite.ui.own;

import java.util.ArrayList;
import java.util.List;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.OwerShareInfo;
import com.ccx.coachsite.bus.ShareInfo;
import com.ccx.coachsite.bus.SoftInfo;
import com.ccx.coachsite.webimage.ImageLoader;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class OwerShareView extends OwerBaseView {

	private ListView mShareList;
	private OwerShareListAdapter mAdapter;
	private ListView mShareTurList;
	private OwerShareTutorialAdapter mTurAdapter;
	private List<ShareInfo> mApp;
	private List<SoftInfo> mTurInfoList;
	private View noneView;

	OwerShareInfo mOwerShareInfo;
	private View btnApp;
	private View btnTur;
	private boolean isAppShow;

	public OwerShareView(Context context, OwerMainBase superView) {
		super(context, superView);

		loadData();
		btnApp = findViewById(R.id.ower_share_app_button);
		btnApp.setOnClickListener(new OnAppShowListener());
		btnTur = findViewById(R.id.ower_share_tur_button);
		btnTur.setOnClickListener(new OnTurListener());
		mShareList = (ListView) findViewById(R.id.ower_share_list);
		mShareTurList = (ListView) findViewById(R.id.ower_share_tur_list);
		noneView = findViewById(R.id.ower_share_none_shared);
		isAppShow = true;
	}

	class OnAppShowListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (isAppShow)
				return;
			setShowSate(!isAppShow);
		}
	}

	class OnTurListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (!isAppShow)
				return;
			setShowSate(!isAppShow);
		}
	}

	private void setShowSate(boolean showApp) {
		isAppShow = showApp;
		btnApp.setSelected(showApp);
		btnTur.setSelected(!showApp);
		if (showApp) {
			mShareTurList.setVisibility(View.GONE);
			if (mApp != null && mApp.size() == 0) {
				noneView.setVisibility(View.VISIBLE);
				mShareList.setVisibility(View.GONE);
			} else {
				noneView.setVisibility(View.GONE);
				mShareList.setVisibility(View.VISIBLE);

			}
		} else {
			mShareList.setVisibility(View.GONE);
			mShareTurList.setVisibility(View.VISIBLE);
			if (mTurInfoList != null && mTurInfoList.size() == 0) {
				noneView.setVisibility(View.VISIBLE);
				mShareTurList.setVisibility(View.GONE);
			} else {
				noneView.setVisibility(View.GONE);
				mShareTurList.setVisibility(View.VISIBLE);
			}
		}
	}

	protected boolean onLoadData() {
		mOwerShareInfo = OwerProvider.getInstance().getShareList(mContext);
		if (mOwerShareInfo == null)
			return false;

		return true;
	}

	protected void doRefreshDataSuccess() {
		TextView lblFavCount = (TextView) findViewById(R.id.ower_share_collect_count);
		TextView lblShareCount = (TextView) findViewById(R.id.ower_share_share_count);
		TextView lblFriendCount = (TextView) findViewById(R.id.ower_share_friend_count);
		lblFavCount.setText(mOwerShareInfo.getFavCount() + "");
		lblShareCount.setText(mOwerShareInfo.getShareCount() + "");
		lblFriendCount.setText(mOwerShareInfo.getShareFriendCount() + "");

		ImageView imgV = (ImageView) findViewById(R.id.ower_share_person_icon);
		imgV.setImageResource(R.drawable.ower_image_none);
		ImageLoader.loadBitmap(mOwerShareInfo.getIcon(), imgV);

		mApp = mOwerShareInfo.getShareList();
		mTurInfoList = mOwerShareInfo.getShareTurList();
		if (mApp == null) {
			mApp = new ArrayList<ShareInfo>();
		}
		if (mTurInfoList == null) {
			mTurInfoList = new ArrayList<SoftInfo>();
		}

		if (mApp != null && mApp.size() > 0) {
			mShareList.setDivider(null);
			mShareList.setOnItemClickListener(new OnItemClickListener());
			mAdapter = new OwerShareListAdapter(mContext, mApp);
			mAdapter.setSuperView(this);
			mShareList.setAdapter(mAdapter);
		}
		if (mTurInfoList != null && mTurInfoList.size() > 0) {
			//mShareTurList.setDivider(null);
			mShareTurList.setDivider(mContext.getResources().getDrawable(
					R.drawable.ower_sip_line));
			mShareTurList.setOnItemClickListener(new OnTurItemClickListener());
			mTurAdapter = new OwerShareTutorialAdapter(mContext, mTurInfoList);
			mShareTurList.setAdapter(mTurAdapter);
		}
		
		setShowSate(isAppShow);
	}

	private class OnTurItemClickListener implements
			android.widget.AdapterView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> adapter, View v, int pos,
				long arg3) {
			if(pos < mTurInfoList.size())
			{
				enterTutorial(mTurInfoList.get(pos).getId(), mTurInfoList.get(pos).getName(), mTurInfoList.get(pos).getIcon());
			}
		}
	}

	private class OnItemClickListener implements
			android.widget.AdapterView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> adapter, View v, int pos,
				long arg3) {

		}
	}

}